package com.yunze.task.yunze.sys;

import com.alibaba.fastjson.JSON;
import com.yunze.common.mapper.mysql.YzCardMapper;
import com.yunze.common.mapper.mysql.sysgl.YzIndexpageMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class IndexpageGenerateTaskMQ {


    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private YzIndexpageMapper yzIndexpageMapper;
    @Resource
    private YzCardMapper yzCardMapper;

    @RabbitHandler
    @RabbitListener(queues = "admin_CompensateGenerateTask_queue")
    public void addPackageProduction(String msg)
    {
        ListeneCommonr(msg);
    }


    private void ListeneCommonr(String msg){
        try {
            if (StringUtils.isEmpty(msg)) {
                return;
            }
            Map<String,Object> Pmap = JSON.parseObject(msg);
            int size = Pmap.get("size")!=null?Integer.parseInt(Pmap.get("size").toString()):3;
            String record_date = Pmap.get("record_date")!=null?Pmap.get("record_date").toString():null;
            if(record_date!=null && record_date.length()>7){
                String yyyyMM = record_date.substring(0,7);
                Map<String, Object> fMap = new HashMap<>();
                fMap.put("yyyyMM",yyyyMM);
                List<String> exDeptArr = yzIndexpageMapper.findToExist(fMap);

                Map<String, Object> fCardMap = new HashMap<>();
                if(exDeptArr!=null){
                    fCardMap.put("deptIdArrs",exDeptArr);
                }
                fCardMap.put("PageSize",size);
                List<String> deptIdArr = yzCardMapper.findDeptid(fCardMap);
                if(deptIdArr!=null && deptIdArr.size()>0){
                    for (int i = 0; i < deptIdArr.size(); i++) {
                        GenerateTask(deptIdArr.get(i),record_date);
                    }
                }
            }

        } catch (Exception e) {
            log.error(">>错误 - 首页数据生成 【ORD】 消费者:{}<<", e.getMessage());
        }
    }



    public void GenerateTask(String dept_id,String record_date) {
        String polling_routingKey = "admin.OrdIndexDataGenerate.queue";
        String polling_exchangeName = "admin_exchange";//路由
        try {
            Map<String, Object> start_Map = new HashMap<>();
            start_Map.put("dept_id", dept_id);//生成企业ID
            start_Map.put("record_date",record_date );//获取前一天的数据
            rabbitTemplate.convertAndSend(polling_exchangeName, polling_routingKey, JSON.toJSONString(start_Map), message -> {
                // 设置消息过期时间 30 分钟 过期
                message.getMessageProperties().setExpiration("" + (30 * 1000 * 60));
                return message;
            });
        } catch (Exception e) {
            System.out.println(" 首页数据生成 指令发送失败 " + e.getMessage());
        }
    }



}
